﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ModelViewer
{
    /// <summary>
    /// Implements the animation tab page control.
    /// </summary>
    public partial class AnimTab : UserControl, TreeViewer.IObserver
    {
        #region Fields

        /// <summary>
        /// The tree viewer this control is associated with
        /// </summary>
        private TreeViewer treeViewer = null;

        #endregion

        #region Properties

        /// <summary>
        /// The associated tree viewer
        /// </summary>
        [Browsable(false)]
        public TreeViewer TreeViewer
        {
            get { return treeViewer; }
            set 
            { 
                treeViewer = value; 
                if (treeViewer != null) 
                    treeViewer.AddObserver(this);
                UpdateUI();
            }
        }

        #endregion


        public AnimTab()
        {
            InitializeComponent();
            UpdateUI();
        }


        public void UpdateObserver()
        {
            UpdateUI();
        }

        private void UpdateUI()
        {
            if (treeViewer == null)
                return;

            ViewerModel model = treeViewer.SelectedModel;
            labelAnimAssign.Enabled = model != null;
            comboAnimAssign.Enabled = model != null;
            comboAnimAssign.Items.Clear();

            // Fill the animation combo box
            foreach (ViewerModel vmodel in treeViewer.Models)
            {
                comboAnimAssign.Items.Add(vmodel.Name);
            }

            if (model != null)
            {
                // If we have a selected player, use it to update
                ClipPlayer player = treeViewer.SelectedClipPlayer;
                if (player != null)
                {
                    comboAnimAssign.Text = player.Model.Name;
                }
                else
                {
                    // No selected model. See if all of the players on that model have the same name
                    if (model.ClipPlayers.Count > 0)
                    {
                        ViewerModel playerModel = model.ClipPlayers[0].Model;
                        foreach (ClipPlayer player1 in model.ClipPlayers)
                        {
                            if (player1.Model != playerModel)
                            {
                                // Different values, so we can't set the selected item
                                return;
                            }
                        }

                        comboAnimAssign.Text = playerModel.Name;
                    }
                }
            }
        }


        private void OnAnimAssignChange(object sender, EventArgs e)
        {
            if (comboAnimAssign.SelectedIndex < 0 || treeViewer.SelectedModel == null)
                return;

            ClipPlayer player = treeViewer.SelectedClipPlayer;
            if (player != null)
            {
                player.Model = treeViewer.Models[comboAnimAssign.SelectedIndex];
            }
            else
            {
                // Set all players on the selected model to this model
                foreach (ClipPlayer setPlayer in treeViewer.SelectedModel.ClipPlayers)
                {
                    setPlayer.Model = treeViewer.Models[comboAnimAssign.SelectedIndex];
                }
            }
        }


    }
}
